<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('helper.inc');
require_once('crypt.inc');
require_once('settings.inc');

$drm = array(true, false, false, false, false, true, false);

$knopf = 0;
$func = $_REQUEST['func'];

# Auswertungsmenue
if (isset($_REQUEST['IST'])) { $knopf = 23; $headline = 25; }

require_once('menu.inc');
require_once('knumber.inc');

# Folgende Funktion erlaubt die Auswahl einer Periode fuer die der IST-Bericht
# erzeugt werden soll.
#
function Preselect() {
	global $periode;
	global $unum;
	global $menu;
	global $rstufe;

	$db = OpenDB();
	$query = "select wd_datum from wdone order by wd_datum";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);

	if ($numrows <= 0) {
	   Error("Es gibt keine IST-Erfassungen!");
	   closeDB($db);
	   return;
	}

	$rows = 0;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $wd_datum = $data[0];

	   if ($rows == 0)
	      $fdate = $wd_datum;

	   $rows++;
	}

	$ldate = $wd_datum;

	switch($periode) {
	   case 1:		// Jahr
	      $sper = GetPeriode($fdate);
	      $eper = GetPeriode($ldate);
	   break;

	   case 2:		// Quartal
	      $sper = 1;
	      $eper = 4;
	   break;

	   case 3:		// Monat
	      $sper = 1;
	      $eper = 12;
	   break;

	   case 4:		// Woche
	      $sper = 1;
	      $eper = 52;
	   break;

	   case 5:		// Tag
	      $sper = 1;
	      $eper = ($ldate - $fdate) / 86400;
	   break;
	}

	$fyear = gmdate("Y", $fdate);
	$eyear = gmdate("Y", $ldate);
?>
<form name="istbericht" action="istbericht.php" method="post">
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="headline" value="25">
<input type="hidden" name="func" value="IST">

<table class="input">
   <tr>
      <td>Periode:</td>
      <td><select name="per">
<?php
	$mon = gmdate("n", time());

	for ($i = $sper; $i <= $eper; $i++) {
	   if ($i == $mon)
	      echo "            <option selected value=\"$i\">$i</option>\n";
	   else
	      echo "            <option value=\"$i\">$i</option>\n";
	}
?>
         </select>
      </td>
   </tr>
<?php
	if ($periode != 1) {
?>
   <tr>
      <td>Jahr:</td>
      <td><select name="year">
<?php
	   $y = gmdate("Y", time());

	   for ($i = $fyear; $i <= $eyear; $i++) {
	      if ($i == $y)
	         echo "<option selected value=\"$i\">$i</option>\n";
	      else
	         echo "<option value=\"$i\">$i</option>\n";
	   }
?>
          </select>
      </td>
   </tr>
<?php
	}

	if (($rstufe >= 0 && $rstufe < 3) || $rstufe == 4 || $rstufe == 6) {
?>
   <tr>
      <td>Mitarbeiter:</td>
      <td><select name="minum">
<?php
	   $query = "select mi_num, mi_vname, mi_nname, kl_land from ";
	   $query .= "mitarbeiter, key_land where kl_num = mi_land and ";
	   $query .= "mi_plan = true ";
	   $query .= "order by mi_nname, mi_vname";
	   $result = QueryDB($db, $query);

	   if (!$result) {
	      closeDB($db);
	      return;
	   }

	   $numrows = numrowsDB($result);
	   $rows = 0;

	   while ($rows < $numrows) {
	      $data = fetchDB($result, $rows);
	      $mi_num = $data[0];
	      $mi_vname = $data[1];
	      $mi_nname = $data[2];
	      $kl_land = $data[3];

	      if ($mi_num == $unum)
	         echo "<option selected value=\"$mi_num\">$mi_nname $mi_vname ($kl_land)</option>\n";
	      else
	         echo "<option value=\"$mi_num\">$mi_nname $mi_vname ($kl_land)</option>\n";

	      $rows++;
	   }
?>
          </select>
	  &nbsp;&nbsp;Alle Mitarbeiter: <input type="checkbox" name="allmi" value="1" checked>
      </td>
   </tr>
<?php
	} else
	   echo "<input type=\"hidden\" name=\"minum\" value=\"$unum\">\n";

	# Auswahl eines Projekts oder auch alle zugleich
	echo "<tr><td>Projektauswahl:</td>\n";
	echo "<td><table border=0 cellspacing=0 cellpadding=0>\n";
	echo "<tr><td><input type=\"radio\" name=\"project\" value=\"1\">Alle Projekte</td>\n";
	echo "<td><input type=\"radio\" name=\"project\" value=\"0\" checked>Projekt:";

	if ($rstufe == 2) {		// Projektleiter
	   $query = "select distinct pr_num, pr_name from ";
	   $query .= "project, plan, task, allocation ";
	   $query .= "where pl_prnum = pr_num and ta_plnum = pl_num and ";
	   $query .= "al_task = ta_num ";
	   $query .= "and (pr_pl = $unum or al_ressource = $unum) ";
	   $query .= "order by pr_num";
	} else if ($rstufe == 3) {	// Mitarbeiter
	   $query = "select distinct wd_prnum, pr_name from ";
	   $query .= "wdone, project where ";
	   $query .= "pr_num = wd_prnum and wd_minum = $unum ";
	   $query .= "order by wd_prnum";
	} else {
	   $query = "select pr_num, pr_name from ";
	   $query .= "project where pr_status = 0 order by pr_num";
	}

	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	echo "<select name=\"selpro\">\n";
	$numrows = numrowsDB($result);
	$rows = 0;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $pr_num = $data[0];
	   $pr_name = $data[1];
	   echo "<option value=\"$pr_num\">$pr_num $pr_name</option>\n";
	   $rows++;
	}

	echo "</select></td></tr></table>\n";
	echo "</td></tr>\n";
	echo "</table>\n";
	echo "<table class=\"indent\"><tr><td>";
	ButtonSubmit("Senden", "istbericht");
	echo "</td></tr></table>";
	echo "</form>\n";
	closeDB($db);
}

function ShowIstErfassung() {
	global $leneinheit;
	global $periode;
	global $rstufe;
	global $unum;

	$per = $_REQUEST['per'];
	$year = $_REQUEST['year'];
	$minum = $_REQUEST['minum'];
	$project = $_REQUEST['project'];
	$selpro = $_REQUEST['selpro'];
	$allmi = $_REQUEST ['allmi'];

	switch($periode) {
	   case 1:		// Jahr
	      $smon = 1;
	      $sday = 1;
	      $emon = 12;
	      $eday = 31;
	   break;

	   case 2:		// Quartal
	      $sday = 1;

	      switch ($per) {
		 case 1:
		    $smon = 1;
		    $emon = 3;
		    $eday = 31;
		 break;

		 case 2:
		    $smon = 4;
		    $emon = 6;
		    $eday = 30;
		 break;

		 case 3:
		    $smon = 7;
		    $emon = 9;
		    $eday = 30;
		 break;

		 case 4:
		    $smon = 10;
		    $emon = 12;
		    $eday = 31;
		 break;
	      }
	   break;

	   case 3:		// Monat
	      $smon = $per;
	      $sday = 1;
	      $emon = $per;
	      $eday = daysinmonth($per, $year);
	   break;

	   case 4:		// Woche
		# FIXME: Currently not implemented
	   break;

	   case 5:		// Tag
		# FIXME: Currently not implemented
	   break;
	}

	$db = OpenDB();
	$anz = 0;

	if ($rstufe == 2) {	// Projektleiter
	   $query = "select distinct pr_num from ";
	   $query .= "project, plan, task, allocation ";
	   $query .= "where pl_prnum = pr_num and ta_plnum = pl_num and ";
	   $query .= "al_task = ta_num ";
	   $query .= "and (pr_pl = $unum or al_ressource = $unum) ";
	   $query .= "order by pr_num";
	   $result = QueryDB($db, $query);

	   if (!$result) {
	      closeDB($db);
	      return;
	   }

	   $numrows = numrowsDB($result);
	   $rows = 0;

	   while ($rows < $numrows) {
	      $data = fetchDB($result, $rows);
	      $pnum[$rows] = $data[0];
	      $rows++;
	   }

	   $anz = $numrows;
	   unset($rows);
	   unset($numrows);
	}

	$fdate = gmmktime(0, 0, 0, $smon, $sday, $year);
	$ldate = gmmktime(0, 0, 0, $emon, $eday, $year);

	$query = "select wd_prnum, wd_datum, wd_hours, wd_phase, wd_rk, ";
	$query .= "wd_minum, mi_nname, mi_vname, pr_name, kp_phase ";
	$query .= "from wdone, mitarbeiter, project, key_phase where ";
	$query .= "wd_datum between $fdate and $ldate and ";

	if (($rstufe > 2 && $rstufe < 4) || !isset($allmi) || $allmi != 1)
	   $query .= "wd_minum = $minum and mi_num = $minum and ";
	else
	   $query .= "mi_num = wd_minum and ";

	$query .= "pr_num = wd_prnum and kp_num = wd_phase and wd_hours > 0 ";

	if (!$project)
	   $query .= " and wd_prnum = $selpro";
	else if ($rstufe == 2) {
	   $query .= " and wd_prnum in (";

	   for ($i = 0; $i < $anz; $i++) {
	      if ($i > 0)
	         $query .= ",";

	      $query .= "$pnum[$i]";
	   }

	   $query .= ") ";
	   unset($pnum);
	}

	$query .= " order by wd_prnum, mi_nname, mi_vname, wd_phase, wd_datum";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);

	if ($numrows <= 0) {
	   Error("F&uuml;r die Periode $per.$year und das gew&auml;hlte Projekt $selpro existiert keine IST-Erfassung!<br>");
	   closeDB($db);
	   return;
	}

	$rows = 0;
	$old_prnum = 0;
	$old_minum = 0;
	$old_phase = 0;
	$old_datum = 0;
	$pos = 1;
	$sum = 0;
	$total = 0;
	$all = 0;
	$flag = false;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $wd_prnum = $data[0];
	   $wd_datum = $data[1];
	   $wd_hours = $data[2];
	   $wd_phase = $data[3];
	   $wd_rk = $data[4];
	   $wd_minum = $data[5];
	   $mi_nname = $data[6];
	   $mi_vname = $data[7];
	   $pr_name = $data[8];
	   $kp_phase = $data[9];

	   if ($old_datum == $wd_datum) {
	      $rows++;
	      continue;
	   } else
	      $old_datum = $wd_datum;

	   if ($old_prnum != $wd_prnum || $old_minum != $wd_minum) {
	      # Die Summenzeile malen sofern es bereits eine gibt.
	      if ($rows > 0) {

		 while ($pos <= 31) {
		    echo "<td class=\"sel\">&nbsp;</td>";
		    $pos++;
		 }

	         printf("<td class=\"selnum\">%s</td>\n", FormatNum($sum, 3));
	         printf("<td class=\"selnum\">%s</td></tr>\n", FormatNum($sum / $leneinheit, 3));
	         $sum = 0;
	         $pos = 1;
	         $old_phase = $wd_phase;
	         $pos = 1;
	         # Summenzeile
		 echo "<tr><td class=\"selnum\" colspan=33><b>Summen:</b></td>\n";
	         printf("<td class=\"selnum\"><b>%s</b></td><td class=\"selnum\"><b>%s</b></td></tr>\n",
	         	FormatNum($total, 3), FormatNum($total / $leneinheit, 3));
		 printf("</table><p class=\"cry\">Zwischensumme: %s, PT: %s</p>\n", FormatNum($all, 3), FormatNum($all / $leneinheit, 3));
		 $total = 0;
		 $flag = true;
	      }

	      # Den Kopf malen
	      echo "<table class=\"sel\" width=\"95%\">\n";
	      echo "<tr><td class=\"sel\">Name:</td>\n";
	      echo "<td style=\"color: #ff0000;\"><b>$mi_nname $mi_vname</b></td><td class=\"sel\"> </td>\n";
	      echo "<td class=\"sel\" colspan=8>Jahr: <b>$year</b>&nbsp;&nbsp;&nbsp;Monat: <b>$per</b></td><td class=\"sel\"> </td>\n";
	      echo "<td class=\"sel\" colspan=23>ProjektNr.: <font color=\"#ff0000\"><b>$wd_prnum</b></font>&nbsp;&nbsp;&nbsp;&nbsp;Projekt: <font color=\"ff0000\"><b>$pr_name</b></font></td></tr>\n";
	      echo "<tr><td class=\"sel\" colspan=2> </td><td class=\"sel\">[Std]</td><td class=\"sel\" colspan=32> </td></tr>\n";
	      echo "<tr><th class=\"sel\">Phase</th><th class=\"sel\">Aktivit&auml;t</th>";

	      for ($i = 1; $i <= 31; $i++)
	         echo "<th class=\"sel\">$i</th>";

	      echo "<th class=\"sel\">Summe<br>in Std</th><th class=\"sel\">Summe<br>in PT</th></tr>\n";
	      $old_prnum = $wd_prnum;
	      $old_minum = $wd_minum;
	   }

	   if ($rows > 0 && $old_phase != $wd_phase) {
	      while ($pos <= 31) {
		 echo "<td>&nbsp;</td>";
		 $pos++;
	      }

	      printf("<td class=\"selnum\">%s</td>\n", FormatNum($sum, 3));
	      printf("<td class=\"selnum\">%s</td></tr>\n", FormatNum($sum / $leneinheit, 3));
	      $sum = 0;
	      $pos = 1;
	      echo "<tr><td class=\"selltgray\">Phase $wd_phase</td><td class=\"selltgray\">$kp_phase</td>\n";
	      $old_phase = $wd_phase;
	      $pos = 1;
	   } else if ($rows == 0 || $flag) {
	      echo "<tr><td class=\"selltgray\">Phase $wd_phase</td><td class=\"selltgray\">$kp_phase</td>\n";
	      $old_phase = $wd_phase;
	      $flag = false;
	   }

	   $p = gmdate("j", $wd_datum);

	   while ($pos < $p) {
	      echo "<td class=\"sel\">&nbsp;</td>";
	      $pos++;
	   }

	   # Die tatsaechliche IST-Summe fuer den aktuellen Tag und Phase
	   # berechnen.
	   $query = "select sum(wd_hours) from wdone where ";
	   $query .= "wd_phase = $wd_phase and wd_minum = $wd_minum and ";
	   $query .= "wd_prnum = $wd_prnum and wd_datum = $wd_datum";

	   if (!($reswd = QueryDB($db, $query))) {
	      closeDB($db);
	      return;
	   }

	   $data = fetchDB($reswd, 0);
	   $wd_hours = $data[0];

	   if ($wd_rk)
	      printf("<td class=\"selnum\">%s<small><b>+<font color=\"#ff0000\">Rk</font></b></small></td>", FormatNum($wd_hours, 1));
	   else
	      printf("<td class=\"selnum\">%s</td>", FormatNum($wd_hours, 1));

	   $sum += $wd_hours;
	   $total += $wd_hours;
	   $all += $wd_hours;
	   $pos++;
	   $rows++;
	}

	while ($pos <= 31) {
	   echo "<td class=\"sel\">&nbsp;</td>";
	   $pos++;
	}

	printf("<td class=\"selnum\">%s</td>\n", FormatNum($sum, 3));
	printf("<td class=\"selnum\">%s</td></tr>\n", FormatNum($sum / $leneinheit, 3));
	echo "<tr><td class=\"selnum\" colspan=33><b>Summen:</b></td>\n";
	printf("<td class=\"selnum\"><b>%s</b></td><td align=\"right\"><b>%s</b></td></tr>\n",
		FormatNum($total, 3), FormatNum($total / $leneinheit, 3));
	echo "</table>\n";
	printf("<p class=\"cry\">Gesamtsumme: %s, PT: %s</p>\n", FormatNum($all, 3), FormatNum($all / $leneinheit, 3));
	closeDB($db);
}

# Auswertung des Menues:
#
if ($knopf == 23)		// IST-Erfassung
   Preselect();

# Auswertung der Funktionen
if ($func == "IST") {
   $per = $_REQUEST['per'];
   $year = $_REQUEST['year'];
   $minum = $_REQUEST['minum'];
   $project = $_REQUEST['project'];
   $selpro = $_REQUEST['selpro'];
   $allmi = $_REQUEST ['allmi'];
   echo "<table class=\"indent\"><tr><td>";
   buttonPrint("Drucken", "istbericht.php", "selpro=$selpro&per=$per&year=$year&minum=$minum&project=$project&allmi=$allmi&header=2&func=PrintIST");
   echo "</td></tr></table>";
//   echo "<a href=\"#\" onClick=\"javascript:open_mwindow('istbericht.php?selpro=$selpro&per=$per&year=$year&minum=$minum&project=$project&allmi=$allmi&header=2&func=PrintIST', 'IST-Bericht', 1000, 650)\">";
//   echo "<img src=\"image/print.png\" border=0 alt=\"Drucken\"></a>\n";

   ShowIstErfassung();
}

if ($func == "PrintIST") {
   echo "<br>\n";
   ShowIstErfassung();
}

require('footer.inc');
?>
